package com.ts.api.module.api.convert.core.utils;

import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.encode.HexUtils;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.ts.api.module.api.convert.core.entity.ApiRequestParameter;
import com.ts.api.module.api.convert.platform.entity.ReqPlatform;

import java.util.Map;

/**
 *  Api对方接入我方加密工具类
 *
 *  @author Pan
 */
public class ApiEncryptUtils {

    /**
     *  撞库/进件通用加密方法
     *
     *  @param  t   实体
     *  @return Map
     *  @param  <T>  ApiRequestMessage父类
     */
    public static <T extends ApiRequestParameter> Map<String, String> encryptData(String key, T t) {
        ReqPlatform apiPlatForm = CopyUtils.copy(t, ReqPlatform.class);
        String json = apiPlatForm.toJson();
        AES ecb = AES.createEcb(HexUtils.decode(key));
        String encrypt = ecb.encryptToHex(json);
        return MapUtils.newHashMap(newMap -> newMap.put("data", encrypt));
    }
}
